package cn.tansanqinger.dao.sql.topic;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ITopicSqlDao;
import cn.tansanqinger.pojo.Topic;

@Repository
public class TopicSqlDaoImpl {
	
	@Autowired
	private ITopicSqlDao dao;
	
	/**
	 * 读取数据
	 * @return
	 */
	public List<Topic> selectTopic(int ttype, int difficulty, int classify, int grade) {
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		if(ttype!=-1) queryWrapper.lambda().eq(Topic::getTtype, ttype);
		if(difficulty!=-1) queryWrapper.lambda().eq(Topic::getTdifficulty, difficulty);
		if(classify!=-1) queryWrapper.lambda().eq(Topic::getTclassify, classify);
		if(grade!=-1) queryWrapper.lambda().eq(Topic::getTgrade, grade);
		queryWrapper.lambda().eq(Topic::getTispopup, 1);
		return dao.selectList(queryWrapper);
	}
	
	/**
	 * 添加信息
	 * @param course
	 * @return
	 */
	public int add(Topic object) {
		return dao.insert(object);
	}
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Topic selectOnt(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 获取编号
	 * @return
	 */
	public Integer serialNumber() {
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		queryWrapper.orderByDesc("tsn").last("limit 1");
		Topic object= dao.selectOne(queryWrapper);
		return object==null?10000001:object.getTsn()+1;
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Topic object) {
		return dao.updateById(object);
	}

	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	public int deleteTeacherUpdata(int id) {
		return dao.deleteById(id);
	}

	/**
	 * 查询教师上传的数据
	 * @param uid
	 * @param page
	 * @return
	 */
	public List<Topic> selectTeacherUpdata(int uid, int page) {
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		queryWrapper.lambda().eq(Topic::getTisidentity, 1).eq(Topic::getTauid, uid);
		Page<Topic> pg = new Page<Topic>(page,10);
		IPage<Topic> iPage = dao.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}

	/**
	 * 查询教师上传的单一数据
	 * @param id
	 * @return
	 */
	public Topic selectTeacherUpdataOne(int id) {
		return dao.selectById(id);
	}

	/**
	 * 查询用户添加数据量
	 * @param id
	 * @return
	 */
	public int topicCountAll(int id) {
		QueryWrapper<Topic> queryWrapper = new QueryWrapper<Topic>();
		queryWrapper.lambda().eq(Topic::getTisidentity, 1).eq(Topic::getTauid, id);
		return dao.selectCount(queryWrapper);
	}
	
}
